package com.liusp.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.liusp.constant.MessageConstant;
import com.liusp.dao.CheckItemDao;
import com.liusp.entity.PageResult;
import com.liusp.entity.Result;
import com.liusp.pojo.CheckItem;
import com.liusp.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * 检查项服务
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;

    /**
     * 新增检查项
     * @param checkItem 新增对象
     */
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    /**
     * 查询体检检查项
     * @return 结果
     */
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String
            queryString) {
            PageHelper.startPage(currentPage,pageSize);
            Page<CheckItem> page = checkItemDao.selectByCondition(queryString);
            return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id删除检查项
     * @param id 主键id
     * @return 结果
     */
    @Override
    public Result delete(Integer id) {
        try {
            //查询是否有关联检查组
            Integer count = checkItemDao.count(id);
            if (count >= 1){
                return new Result(false, "当前检查项被引用，不能删除!");
            }
            //删除检查项
            int row = checkItemDao.delete(id);
            if (row < 1){
                return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
            }
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    /**
     * 根据id查询检查项
     * @param id 主键id
     * @return 结果
     */
    @Override
    public Result findById(Integer id) {
        try {
            CheckItem checkItem = checkItemDao.findById(id);
            if (checkItem == null){
                return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL,null);
            }
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
        } catch (Exception e) {
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL,null);
        }
    }

    /**
     * 编辑检查项
     * @param checkItem 编辑对象
     * @return 结果
     */
    @Override
    public Result edit(CheckItem checkItem) {
        try {
            int row = checkItemDao.edit(checkItem);
            if (row == 0){
                return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
            }
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    /**
     * 查询所有检查项
     * @return 结果
     */
    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
}